package com.icxcc.generator.service.impl;


import com.icxcc.common.framework.service.SuperServiceImpl;
import com.icxcc.common.util.R;
import com.icxcc.generator.annotation.DbType;
import com.icxcc.generator.config.*;
import com.icxcc.generator.config.converts.MySqlTypeConvert;
import com.icxcc.generator.config.rules.DbColumnType;
import com.icxcc.generator.config.rules.NamingStrategy;
import com.icxcc.generator.engine.AutoGenerator;
import com.icxcc.generator.engine.FreemarkerTemplateEngine;
import com.icxcc.generator.mapper.GeneratorMapper;
import com.icxcc.generator.service.GeneratorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("generatorService")
@Slf4j
public class GeneratorServiceImpl extends SuperServiceImpl implements GeneratorService {
    @Autowired
    private GeneratorConfigProperties generatorConfigProperties;
    @Autowired
    public GeneratorServiceImpl(GeneratorMapper generatorMapper) {
        super(generatorMapper);
    }

    /**
     * 生成代码
     * @param tables
     * @return
     */
    @Override
    public R generator(String... tables) {
        AutoGenerator ag = new AutoGenerator();
        ag.setTemplateEngine(new FreemarkerTemplateEngine());
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(generatorConfigProperties.getOutputDir());
        gc.setFileOverride(generatorConfigProperties.isFileOverride());
        gc.setActiveRecord(generatorConfigProperties.isActiveRecord());
        gc.setEnableCache(generatorConfigProperties.isEnableCache());
        gc.setBaseResultMap(generatorConfigProperties.isBaseResultMap());
        gc.setBaseColumnList(generatorConfigProperties.isBaseColumnList());
        gc.setAuthor(generatorConfigProperties.getAuthor());
        ag.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setDbType(DbType.MYSQL);
        dataSource.setTypeConvert(new MySqlTypeConvert(){
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                return super.processTypeConvert(globalConfig, fieldType);
            }
        });
        dataSource.setDriverName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://47.98.183.73:3306/oauth2db?characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("cxadmin123");
        ag.setDataSource(dataSource);

        //策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setTablePrefix("sys_");
        sc.setNaming(NamingStrategy.underline_to_camel);//表名生成策略
        ag.setStrategy(sc);
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.icxcc");
        pc.setModuleName("test");
        ag.setPackageInfo(pc);
        ag.execute();
        return null;
    }

    @Override
    public void getInfo() {
        log.info(generatorConfigProperties.getAuthor());
    }
}
